#ifndef __PERSISTENTQ_H__
#define __PERSISTENTQ_H__


#include "CommonHeader.h"


enum QMode
{
    eUnknown,
    eWriter,
    eReader,
    eBoth,
};

class PersistentQ
{
public:

    PersistentQ();

    ~PersistentQ();

    //Get the front data from  queue and on success return true;
    bool GetFront(Data&);
    //put  data  on the back of the queue and retrun true on success

    bool PushBack(Data&);
    //get current entry count
    int size();

    //restore queue data from last state and init the queue
    bool init(const char *,QMode = eWriter );
    //close file handlers and finish memory maping
    void close();

    HANDLE OpenOrCreateFile(const char*);

private:

    //allocate block in the mapped file
    void* GetBlock(size_t );

    //deallocate block in the mapped file
    void ReleaseBlock(void* );
    //
    bool OpenMappedFileWriter(const char*);

    bool OpenMappedFileReader(const char*);

    PersistentQ(const PersistentQ&);

    PersistentQ operator = (const PersistentQ&);

    //file name =>path
    char* 		m_pcFileName;

    //memory mapped file handle
    HANDLE		m_hMapFile;

    //mapped address start
    void* 		m_pBuf;

    // allocation start for mapped address
    void* 	  	m_pBufAllocation;
    //Hard dsik file handle
    HANDLE		m_hFile;
    //entry count

    int*         m_pnCount;
    //head
    int*         m_pnHeadIndex;
    //tail
    int*         m_pnTailIndex;
    // mode of operation
    QMode       m_eMode;

    int         m_nElemCount;
};
#endif //__PERSISTENTQ_H__
